package DesginFotmat;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TwoPhaseStop2 {
    public static void main(String[] args) throws InterruptedException {
        MonitorThread2 monitorThread = new MonitorThread2();
        monitorThread.start();

        Thread.sleep(3500);
        log.debug("停止监控");
        monitorThread.stop();
    }
}

@Slf4j
class MonitorThread2{
    //监控线程
    private Thread monitorThread;

    //停止标记
    private volatile boolean stop = false;

    //启动监控线程
    public void start(){
        monitorThread = new Thread(()->{
            while(true){
                Thread current = Thread.currentThread();
                //是否被打断
                if(stop){
                    log.debug("料理后事");
                    break;
                }
                try{
                    Thread.sleep(1000);
                    log.debug("执行监控记录");
                }catch(InterruptedException e){

                }
            }
        },"MonitorThread");
        monitorThread.start();
    }


    //停止监控线程
    public void stop(){
        stop = true;
        monitorThread.interrupt();
    }
}
